# -*- coding:utf-8 -*-
# 北梦测教育
# 课程咨询加微信：xiaobeiceshi
from flask import Flask, jsonify, request
from config.path_manage import get_mock_data_path
import json

# 实例化一个flask应用
app = Flask(__name__)

# 定义一个mock地址的常量
MOCK_DATA_FILE = get_mock_data_path()

# 读取mock的数据配置文件
with open(MOCK_DATA_FILE, "r") as file:
    mock_data = json.load(file)


# /<path:path> 这个表示接受任何路由地址
@app.route('/<path:path>', methods=["GET", "POST", "PUT", "DELETE"])
def mock_api(path):
    # 拼接配置文件的键
    method = request.method

    key = method + f" {path}"

    # 遍历配置文件里面的key和value
    for k, value in mock_data.items():
        if key == k:  # 判断请求方式和路径是否对应
            status_code = value.get("status_code", 200)
            response = value.get("response", {})

            # 返回数据以及http状态码
            return jsonify(response), status_code


if __name__ == '__main__':
    app.run(debug=True)
